﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpBolt.Graphics;
using SharpBolt.Basics;
using InductBase;
using System.IO;
using System.ComponentModel;
using Newtonsoft.Json;

namespace SharpBolt.Graphics.GraphicSources
{
    [JsonObject]
    [Editor(typeof(Editing.UIImageSourceEditor), typeof(System.Drawing.Design.UITypeEditor))]
    public class ImageSource : IGraphicSource
    {
        public SbBitmap Bitmap { get; private set; }
        public string Name { get; protected set; }
        private ImageSource()
        {

        }

        public static ImageSource LoadImage(string name)
        {
            if (!Engine.Miscs.IsResourceExist(name)) return null;

            ImageSource source = new ImageSource();
            source.Name = name;
            source.Bitmap = Engine.GraphicRender.GetBitmap(name);
            return source;
        }

        public static void FreeImage(string name)
        {
            Engine.GraphicRender.FreeBitmap(name);
        }

        public void Draw(Painter painter, Vec2 position)
        {
            painter.DrawBitmap(Bitmap,
                new Transformation(position, new Vec2(1, 1), Vec2.Null, 0),
                Colors.White);
        }

        public void Draw(Painter painter, Vec2 position, Color color)
        {
            painter.DrawBitmap(Bitmap,
                new Transformation(position, new Vec2(1, 1), Vec2.Null, 0),
                color);
        }

        public void Draw(Painter painter, InductBase.Transformation transformation)
        {
            painter.DrawBitmap(Bitmap, transformation, Colors.White);
        }

        public void Draw(Painter painter, InductBase.Transformation transformation, Color color)
        {
            painter.DrawBitmap(Bitmap, transformation, color);
        }

        [JsonIgnore]
        public Vec2 Size
        {
            get
            {
                return Bitmap.Size;
            }
        }

        public override string ToString()
        {
            return Name;
        }
    }
}
